package com.me.jingdongshop.ui.fragment;

import android.content.Intent;
import android.os.Message;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.liaoinstan.springview.container.AliHeader;
import com.liaoinstan.springview.widget.SpringView;
import com.me.common.base.BaseFragment;
import com.me.jingdongshop.R;
import com.me.jingdongshop.dagger2.component.DaggerHomeFragmentComponent;
import com.me.jingdongshop.dagger2.module.HomeFragmentModule;
import com.me.jingdongshop.model.bean.Home;
import com.me.jingdongshop.model.protocol.IHttpService;
import com.me.jingdongshop.presenter.HomeFragmentPresenter;
import com.me.jingdongshop.ui.activity.HomeHeadScanActivity;
import com.me.jingdongshop.ui.activity.HomeSearchPagerActivity;
import com.me.jingdongshop.ui.activity.MessageCenterActivity;
import com.me.jingdongshop.ui.adapter.HomeAdapter;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

/**
 * Created by wuer on 2017/5/5/005.
 * 首页模块
 */
public class HomeFragment extends BaseFragment {
    private LinearLayout llSearchHeadParent;
    private LinearLayout llHomeHeadSearch;
    private ImageView ivHeadScan;
    private ImageView ivSearchHead;
    private TextView tvSearchHead;
    private ImageView ivVoice;
    private ImageView ivHeadMessage;
    private SpringView springView;
    private RecyclerView recyclerView;

    private Home home;
    private HomeAdapter homeAdapter;

    @Override
    public int getLayoutRes() {
        return R.layout.fragment_home;
    }

    @Override
    public void initView() {
        //头部搜索
        llSearchHeadParent = (LinearLayout) findView(R.id.ll_search_head_parent);
        llHomeHeadSearch=(LinearLayout) findView(R.id.ll_home_head_search);
        llHomeHeadSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(v.getContext(), HomeSearchPagerActivity.class);
                v.getContext().startActivity(intent);
            }
        });
        ivHeadScan = (ImageView) findView(R.id.iv_head_scan);
        ivHeadScan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(v.getContext(), HomeHeadScanActivity.class);
                v.getContext().startActivity(intent);
            }
        });
        ivSearchHead = (ImageView) findView(R.id.iv_search_head);
        tvSearchHead = (TextView) findView(R.id.tv_search_head);
        ivVoice = (ImageView) findView(R.id.iv_voice);
        ivHeadMessage = (ImageView) findView(R.id.iv_head_message);
        ivHeadMessage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(v.getContext(), MessageCenterActivity.class);
                v.getContext().startActivity(intent);
            }
        });

        //上拉刷新，下拉加载
        springView = (SpringView) findView(R.id.spring_view);
        recyclerView = (RecyclerView) findView(R.id.recycler_view);
        initSpringView();
        initRecyclerView();
    }

    @Inject
    HomeFragmentPresenter mPresenter;

    private void initSpringView() {
        springView.setHeader(new AliHeader(mActivity));
        //springView.setFooter(new AliFooter(mActivity));
        springView.setType(SpringView.Type.FOLLOW);
        springView.setListener(new SpringView.OnFreshListener() {

            @Override
            public void onRefresh() {
                // 重新获取头部数据，然后会加载第一页数据
                mPresenter.getHomeData();

            }

            @Override
            public void onLoadmore() {
                // 加载下一页数据
                mPresenter.getHomeData();
            }
        });
    }

    private ArrayList listDatas = new ArrayList();

    private void initRecyclerView() {
        recyclerView.setLayoutManager(new LinearLayoutManager(mActivity));
        homeAdapter = new HomeAdapter(mActivity, listDatas);
        recyclerView.setAdapter(homeAdapter);
    }

    @Override
    public void initListener() {

    }

    @Override
    public void initData() {
        DaggerHomeFragmentComponent.builder().
                homeFragmentModule(new HomeFragmentModule(this)).build().inject(this);

        mPresenter.getHomeData();
    }

    @Override
    public void onClick(View v, int id) {

    }

    @Override
    public void onHttpSuccess(int reqType, Message msg) {
        if (reqType == IHttpService.HTTP_HOME) {
            // 隐藏下拉刷新的加载更多
            springView.onFinishFreshAndLoad();

            home = (Home) msg.obj;
            List<Home.HomeTopicBean> homeTopic = home.getHomeTopic();
            List<List<Home.HomeTopicBean>> list = new ArrayList<>();
            list.add(homeTopic);
            // 拿到列表的每个item的所有数据
            //recyclerView.setAdapter(new HomeAdapter(getContext(), list));
            homeAdapter.setDatas(list);

        }
    }

    @Override
    public void onHttpError(int reqType, String error) {

    }
}
